
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' A Groups blog message
	''' </summary>
	Public Class GroupBlog
		Inherits BaseBlog
		Private groupID As Integer = -1

		Public Sub New(ByVal dr As SqlDataReader)
			MyBase.New(dr)
		End Sub

		' Use to post blog comment
		' Use approved = false, if the comment has to be approved first
		Public Sub New(ByVal groupID As Integer, ByVal parentID As Integer, ByVal fromUID As Integer, ByVal subject As String, ByVal body As String, ByVal approved As Boolean)
			MyBase.New(parentID, fromUID, subject, body)
			Me.groupID = groupID

			Me.status = IIf(approved,BlogStatus.gbsApproved,BlogStatus.gbsInitial)
		End Sub

		' Use to post new blog
		Public Sub New(ByVal groupID As Integer, ByVal fromUID As Integer, ByVal subject As String, ByVal body As String)
			MyBase.New(fromUID, subject, body)
			Me.groupID = groupID
			Me.status = BlogStatus.gbsApproved
		End Sub

		Public ReadOnly Property GroupID() As Integer
			Get
				Return groupID
			End Get
		End Property

		Public Overloads Overrides Sub Delete()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				Dim cmd As SqlCommand

				If parentID < 0 Then
					' It's a root blog - delete all children first
					cmd = New SqlCommand("DELETE FROM GroupBlogs WHERE ParentID=@ParentID")
					cmd.Connection = conn
					cmd.Transaction = tran
					cmd.Parameters.Add("@ParentID", blogID)
					cmd.ExecuteNonQuery()
				End If

				cmd = New SqlCommand("DELETE FROM GroupBlogs WHERE ID=@ID")
				cmd.Connection = conn
				cmd.Transaction = tran
				cmd.Parameters.Add("@ID", blogID)
				cmd.ExecuteNonQuery()

				tran.Commit()
			Catch generatedExceptionName As Exception
				tran.Rollback()
				Throw
			Finally
				conn.Close()
			End Try
		End Sub

		Public Overloads Overrides Sub Save()
			If blogID < 0 Then
				Throw New MessageException("New Blog entries may not be modified. Use Send to insert a new entry.")
			End If

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				Dim cmd As New SqlCommand("UPDATE GroupBlogs SET Subject=@Subject, Body=@Body, LastUpdated=@LastUpdated, Status=@Status WHERE ID=@ID")
				cmd.Connection = conn
				cmd.Transaction = tran

				cmd.Parameters.Add("@ID", blogID)

				cmd.Parameters.Add("@Subject", subject)
				cmd.Parameters.Add("@Body", body)
				cmd.Parameters.Add("@LastUpdated", DateTime.Now)

				cmd.Parameters.Add("@Status", DirectCast(status, Integer))

				cmd.ExecuteNonQuery()
				tran.Commit()
			Catch generatedExceptionName As Exception
				tran.Rollback()
				Throw
			Finally
				conn.Close()
			End Try

			If parentID <> -1 Then
				' Send a notification
				Dim grp As Group = Groups.GetGroupByID(groupID)
				If grp.Uid <> senderUID Then
					Groups.SendBlogUpdateNotification(grp, Me)
				End If
			End If
		End Sub

		Public Overloads Overrides Sub Send()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand("SaveGroupBlog")
				cmd.CommandType = CommandType.StoredProcedure
				cmd.Connection = conn

				cmd.Parameters.Add("@GroupID", groupID)
				DBManager.SetNZValue(cmd, "@ParentID", parentID)
				cmd.Parameters.Add("@Subject", subject)
				cmd.Parameters.Add("@Body", body)
				cmd.Parameters.Add("@PostedByUID", senderUID)
				cmd.Parameters.Add("@Status", DirectCast(status, Integer))
				cmd.Parameters.Add("@Date", DateTime.Now)

				Dim paramID As SqlParameter = cmd.Parameters.Add("@ID", SqlDbType.Int)
				paramID.Direction = ParameterDirection.Output

				cmd.ExecuteNonQuery()

				blogID = DirectCast(paramID.Value, Integer)
			Finally
				conn.Close()
			End Try

			If parentID <> -1 Then
				' Send a notification
				Dim grp As Group = Groups.GetGroupByID(groupID)
				If grp.Uid <> senderUID Then
					Groups.SendBlogNotification(grp, Me)
				End If
			End If
		End Sub

		'
		' Private methods
		'

		Protected Overloads Overrides Sub Init(ByVal dr As SqlDataReader)
			blogID = dr.GetInt32(dr.GetOrdinal("ID"))
			senderUID = dr.GetInt32(dr.GetOrdinal("PostedByUID"))
			parentID = DBManager.GetNZInt32(dr, "ParentID")
			subject = dr.GetString(dr.GetOrdinal("Subject"))
			body = dr.GetString(dr.GetOrdinal("Body"))
			dateSent = dr.GetDateTime(dr.GetOrdinal("Date"))

			groupID = dr.GetInt32(dr.GetOrdinal("GroupID"))
			status = DirectCast(dr.GetInt32(dr.GetOrdinal("Status")), BlogStatus)
			

		End Sub
	End Class
End Namespace






